Table of Contents
Page Header & Footer

Overview

Note: There are several different ways to implement page headers and footers. This topic demonstrates how to create page header and footer with child AcmRender objects. Two other methods can be found here and here.

Using Child AcmRender

To use a child AcmRender object to render page header and footer, first you must handle the AcmRender's BeforePageRender or AfterPageRender event. Either event will work. The difference is contents that are rendered first are placed at the "bottom" on the z-order. Thus contents rendered inside BeforePageRender will appear below the regular page contents, while the regular page contents will appear below contents rendered inside AfterPageRender.

The following code demonstrates how to handle BeforePageRender event ( AfterPageRender is the same):

private void CreatePdf()
{
    //Create a new Pdf document
    PdfDocument doc = new PdfDocument();

    //Create a new AcmRender object
    AcmRender render = new AcmRender(doc);

    //Handle the render's BeforeRenderPage event
    render.BeforeRenderPage += new AcmPageEventHandler(BeforeRenderPage);

    //Create some contents
    AcmContent content = new AcmContent(new AcmText("Hello!"));

    //Render the contents
    render.Render(content);

    //Save output
    doc.Save("hello.pdf");
}

private void BeforeRenderPage(object sender, AcmPageEventArgs e)
{
    //Get the page object
    PdfPage page = e.Page;

    //Create page header and footer here
    ....
}

To create page header and footer, simply creating another AcmRender object and renders contents on the new page at the desired place:

//Create a new AcmRender object. Note this render object 
//takes three arguments, all are important for rendering 
//page header and footer. The first two arguments specify 
//start page and the start position of this render. The 
//third argument sets the page margin to 0 on the left 
//and right so that we can render anywhere on the page. 
//Note this margin setting is only being used by this 
//render and does not affect the parent render object
AcmRender render = new AcmRender(
   page, 0, new AcmPageLayout(new AcmPadding(1, 0, 1, 0)));

AcmBlock header = new AcmBlock(
   new AcmText(string.Format("Page Header - Page {0}", page.Index)));
header.Style.Border.Bottom = new AcmLineInfo(AcmLineStyle.Solid, Color.Black, 0.01f);
header.Style.Top = 0.6f;

AcmBlock footer = new AcmBlock(
   new AcmText(string.Format("Page footer - Page {0}", page.Index)));
footer.Style.Border.Top = new AcmLineInfo(AcmLineStyle.Solid, Color.Black, 0.01f);
footer.Style.Top = 10.4f;

render.Render(header, footer);

The above code creates a new AcmRender object, then renders two floated blocks to the page. One is positioned at the top of the page (as page header) and another is positioned at the bottom of the page (as page footer). It also draws a horizontal bottom line for the top block and a horizontal top line for the bottom block.

Using Using Tag to Store Context Information

You can use AcmContent.Tag property to store context information about that content. For example, you can store chapter name in this property and then use this information to render the chapter name as page header.

private void CreatePdf()
{   
    //More code....
     
    //The root content for chapter 1      
    AcmContent chapter1 = new AcmContent();
    chapter1.Tag = "Chatper 1 - Overview";

    //Creating contents for chapter 1
    ....
    
    //More code...
}

private void BeforeRenderPage(object sender, AcmRenderEventArgs e)
{
    //Get the first leaf content in the page
    AcmContent content = e.FirstPageContent;

    //Walk up the parent chain until we find the root
    //content for the chapter and get the value of the
    //Tag property, which we used to store chapter name
    string tag = null;
    while (content != null)
    {
        if (content.Tag != null)
        {
            tag = (string)content.Tag;
            break;
        }

        content = content.Parent;
    }
    
    //More code to render Tag as page header....
    ....
}